// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// d [author] rhyme_lph
// S [email]  rhymelph@gmail.com
// L [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/contact_person_entity.dart';
import 'package:finance_app/src/widgets/button/normal_border_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/phone_number_input_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/contact/new_contact_person_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:tuple/tuple.dart';
export 'package:finance_app/src/entities/contact_person_entity.dart';

@RouterMeta(paramName: "newContactPersonPage")
class NewContactPersonPage extends StatefulWidget {
  final String name;
  final String phoneNumber;
  final int id;
  final ContactPersonEntity data;

  const NewContactPersonPage(
      {Key key, this.name, this.phoneNumber, this.id, this.data})
      : super(key: key);

  @override
  _NewContactPersonPageState createState() => _NewContactPersonPageState();
}

class _NewContactPersonPageState extends State<NewContactPersonPage>
    with PagePresenter<NewContactPersonPage, NewContactPersonProvider> {
  @override
  void initState() {
    provider.contactId = widget.id;
    if (widget.name != null) {
      provider.nameController.text = widget.name;
    }
    if (widget.phoneNumber != null) {
      provider.phoneNumberController.text = widget.phoneNumber;
    }
    provider.initContactPerson(widget.data);
    super.initState();
  }

  String get title => widget.data == null
      ? S.current.new_contact_person
      : provider.isEdit == true
          ? widget.data == null
              ? S.current.edit_contact_person
              : S.current.edit + ' ' + widget.data?.name
          : S.current.contact_person;

  Widget get rightButton => listBuilder<bool>(
        selector: (_, __) => __.isEdit,
        builder: (BuildContext context, bool value, Widget child) =>
            widget.data != null && provider.isEdit == false
                ? _buildPopButton()
                : _buildSaveBtn(),
      );

  Widget _buildPopButton() => listBuilder<Tuple2<bool, List<String>>>(
        selector: (_, __) => Tuple2(
            provider.id != null && __.isEdit == true, List.from(__.menuList)),
        builder: (BuildContext context, Tuple2<bool, List<String>> value,
                Widget child) =>
            value.item1 == true
                ? Container()
                : NormalMorePopupButton(
                    onSelect: provider.onSelectMenu,
                    menuList: provider.menuList,
                  ),
      );

  Widget _buildSaveBtn() {
    return listBuilder<bool>(
      selector: (_, __) => pageStatus != PageStatus.loading,
      builder: (BuildContext context, bool value, Widget child) => value == true
          ? NormalSaveTextButton(
              onTap: provider.onSave,
            )
          : Container(),
    );
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          titleWidget: listBuilder<bool>(
            selector: (_, __) => __.isEdit,
            builder: (BuildContext context, bool value, Widget child) =>
                Text(title),
          ),
          actions: [
            rightButton,
          ]);

  @override
  Widget wrapperBody(Widget child) {
    return listBuilder<bool>(
      selector: (_, __) => __.isEdit == true || provider.id == null,
      builder: (BuildContext context, bool value, Widget child) =>
          FormEditStatusInheritedWidget(
        isEdit: value,
        child: child,
      ),
      child: child,
    );
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            FromInputField(
              tip: S.current.name,
              controller: provider.nameController,
              focusNode: provider.nameFocusNode,
            ),
            PhoneNumberInputForm(
              controller: provider.phoneNumberController,
              focusNode: provider.phoneNumberFocusNode,
              countryCodeCallback: provider.updateCountryCode,
              defaultCountryCode: provider.countryCode,
            ),
            FromInputField(
              tip: S.current.email,
              controller: provider.emailController,
              focusNode: provider.emailFocusNode,
            ),
            FromInputField(
              tip: S.current.position,
              controller: provider.positionController,
              focusNode: provider.positionFocusNode,
            ),
            listBuilder<bool>(
              selector: (_, __) => __.isCheckPrimary,
              builder: (BuildContext context, bool value, Widget child) =>
                  FormStatusWidget(
                builder: (enable) => NormalFormWrapper(
                  child: PressedOpacityButton(
                    onPressed: enable ? provider.onChangePrimary : () {},
                    child: NormalCheckBox(
                      title: S.current.primary_contact,
                      isSelect: value,
                      isEnable: enable,
                    ),
                  ),
                ),
              ),
            ),
            FormStatusWidget(
              builder: (enable) => Column(
                children: !enable
                    ? [
                        NormalBorderButton(
                          isFit: true,
                          title: S.current.call,
                          onTap: provider.onCall,
                        ),
                        AppSpaces.h15,
                        NormalBorderButton(
                          isFit: true,
                          title: S.current.email,
                          onTap: provider.onEmail,
                        ),
                      ]
                    : [],
              ),
            )
          ],
        ),
      );
}
